package com.jyw.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyw.common.utils.DateUtils;
import com.jyw.system.domain.SysConfig;
import com.jyw.system.domain.SysKnowledgeReviewConfig;
import com.jyw.system.domain.dto.SysKnowledgeReviewConfigAddDto;
import com.jyw.system.domain.dto.SysKnowledgeReviewConfigEditDto;
import com.jyw.system.domain.dto.SysKnowledgeReviewConfigQueryDto;
import com.jyw.system.domain.vo.SysKnowledgeReviewConfigVo;
import com.jyw.system.domain.vo.SysKnowledgeReviewDetailVo;
import com.jyw.system.enums.OtherConfigEnum;
import com.jyw.system.mapper.SysKnowledgeReviewConfigMapper;
import com.jyw.system.service.ISysConfigService;
import com.jyw.system.service.ISysKnowledgeReviewConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Created by ZhangCheng on 2024/01/15
 */
@Service
public class SysKnowledgeReviewConfigServiceImpl extends ServiceImpl<SysKnowledgeReviewConfigMapper, SysKnowledgeReviewConfig> implements ISysKnowledgeReviewConfigService {

    @Resource
    private SysKnowledgeReviewConfigMapper sysKnowledgeReviewConfigMapper;

    @Resource
    private ISysConfigService sysConfigService;

    @Override
    public SysKnowledgeReviewDetailVo getDefaultConfig() {
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.KNOWLEDGE_REVIEW.getKey());
        if (sysConfig != null && StrUtil.isNotEmpty(sysConfig.getConfigValue())) {
            return JSONObject.parseObject(sysConfig.getConfigValue(), SysKnowledgeReviewDetailVo.class);
        }
        // 默认开启
        SysKnowledgeReviewDetailVo reviewDetailVo = new SysKnowledgeReviewDetailVo();
        reviewDetailVo.setEnabled(true);
        return reviewDetailVo;
    }

    @Override
    public void updateConfig(SysKnowledgeReviewDetailVo sysKnowledgeReviewConfigVo) {
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.KNOWLEDGE_REVIEW.getKey());
        String configValue = JSONObject.toJSONString(sysKnowledgeReviewConfigVo);
        if (sysConfig != null) {
            sysConfig.setConfigValue(configValue);
            sysConfig.setUpdateTime(DateUtils.getNowDate());
            sysConfigService.updateConfig(sysConfig);
        } else {
            sysConfigService.initConfig(configValue, OtherConfigEnum.KNOWLEDGE_REVIEW);
        }
    }

    @Override
    public List<SysKnowledgeReviewConfigVo> queryFaultTypeList(SysKnowledgeReviewConfigQueryDto queryDto) {
        return sysKnowledgeReviewConfigMapper.queryFaultTypeList(queryDto);
    }

    @Override
    public void add(SysKnowledgeReviewConfigAddDto addDto) {
        SysKnowledgeReviewConfig knowledgeReview = BeanUtil.copyProperties(addDto, SysKnowledgeReviewConfig.class);
        this.save(knowledgeReview);
    }

    @Override
    public void edit(SysKnowledgeReviewConfigEditDto editDto) {
        SysKnowledgeReviewConfig knowledgeReview = this.getById(editDto.getId());
        if (knowledgeReview != null) {
            BeanUtil.copyProperties(editDto, knowledgeReview);
            this.updateById(knowledgeReview);
        }
    }

    @Override
    public SysKnowledgeReviewConfig findByFaultType(String faultType) {
        return this.baseMapper.findByFaultType(faultType);
    }
}
